package design_pattern.ProductConsumeMode;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: 蔡翔
 * @Date: 2019/11/15 9:08
 * @Version 1.0
 */
public class ProducerThread extends Thread {
    public final MessageQueue messageQueue;
    public ProducerThread(MessageQueue messageQueue,int seq){
        super("PRODUCER "+seq);
        this.messageQueue = messageQueue;
    }

    private static final Random random = new Random(System.currentTimeMillis());
    // 用static 始终只有一个
    private final static AtomicInteger counter = new AtomicInteger(0);
    @Override
    public void run() {
        while (true){
            try {
                Message message = new Message("Messag3e-" + counter.getAndIncrement());
                messageQueue.put(message);
                System.out.println(Thread.currentThread().getName()+" put message "+message.getData());
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
